<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:jsd="http://www.cycle99.com/tools/ecmadoc"
	xmlns:math="http://www.cycle99.com/tools/ecmadoc/transform/math"
	xmlns:help="http://www.cycle99.com/tools/ecmadoc/transform/help"
	xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="jsd math help">

	<xsl:include href="common.xslt"/>

	<xsl:template match="/" mode="head-current">
		<script type="text/javascript" language="javascript" src="scripts/toc.js"></script>
	</xsl:template>

	<xsl:template match="/" mode="content">
		<div id="toc">
			<xsl:apply-templates select="project" mode="toc-node">
				<xsl:with-param name="count" select="1"/>
				<xsl:with-param name="root" select="1"/>
			</xsl:apply-templates>
		</div>
	</xsl:template>

	<xsl:template match="/" mode="title-page">
		Contents
	</xsl:template>

	<xsl:template match="*" mode="toc-node">
		<xsl:param name="count" select="count(parent::*/*[not(@inheritedFrom)])"/>
		<xsl:param name="position" select="position()"/>
		<xsl:param name="root" select="0"/>

		<xsl:variable name="node" select="name()"/>
		<xsl:variable name="childCount">
			<xsl:apply-templates select="." mode="toc-node-childcount"/>
		</xsl:variable>

		<xsl:variable name="classNameNode">
			<xsl:if test="$root = 1">root </xsl:if>
			<xsl:if test="$position = $count">l</xsl:if>
			<xsl:if test="$position != $count">s</xsl:if>
			<xsl:choose>
				<xsl:when test="$childCount = 0">s</xsl:when>
				<xsl:when test="$childCount != 0">g</xsl:when>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="control">
			<xsl:if test="$childCount != 0">plus</xsl:if>
		</xsl:variable>

		<xsl:variable name="itemtype">
			<xsl:apply-templates select="." mode="toc-node-item-type"/>
		</xsl:variable>

		<div class="node {$classNameNode} {name()}">
			<div class="header">
				<div class="control {$control}">&#160;</div>
				<xsl:apply-templates select="." mode="icon">
					<xsl:with-param name="itemtype" select="$itemtype"/>
				</xsl:apply-templates>
				<div class="title">
					<xsl:apply-templates select="." mode="toc-node-title"/>
				</div>
			</div>
			<xsl:if test="$childCount != 0">
				<div class="children">
					<xsl:apply-templates select="." mode="toc-node-children"/>
				</div>
			</xsl:if>
		</div>
	</xsl:template>

	<xsl:template match="*[parent::members]" mode="toc-node-title">
		<xsl:variable name="url" select="concat(parent::members/parent::*/@url, '#', name())"/>
		<xsl:variable name="name" select="help:ToUpperCaseFirst(name())"/>
		<a href="{$url}" target="main">
			<xsl:value-of select="$name"/>
		</a>
	</xsl:template>

	<xsl:template match="*[parent::members]" mode="toc-node-item-type">
		<xsl:text>folder</xsl:text>
	</xsl:template>

	<xsl:template match="*[parent::members]" mode="toc-node-childcount">
		<xsl:value-of select="count(*[not(@inheritedFrom)])"/>
	</xsl:template>

	<xsl:template match="*[parent::members]" mode="toc-node-children">
		<xsl:apply-templates select="*[not(@inheritedFrom)]" mode="toc-node">
			<xsl:sort select="@sort" data-type="number" order="ascending" />
			<xsl:sort select="@name" data-type="text" order="ascending" />
		</xsl:apply-templates>
	</xsl:template>

	<xsl:template match="*" mode="toc-node-title">
		<a href="{@url}" target="main">
			<xsl:if test="@virtual">
				<xsl:attribute name="class">virtual</xsl:attribute>
				<xsl:attribute name="title">This object appears in code but its definition could not be found.</xsl:attribute>
			</xsl:if>
			<xsl:value-of select="help:Iif(@visibility = 'static', @staticName, @name)"/>
		</a>
	</xsl:template>

	<xsl:template match="*" mode="toc-node-childcount">
		<xsl:value-of select="count(members/*/*)"/>
	</xsl:template>

	<xsl:template match="*" mode="toc-node-children">
		<xsl:choose>
			<xsl:when test="$groupBy = 'type' and count(members/*[count(*[not(@inheritedFrom)]) != 0]) > 1">
				<xsl:apply-templates select="members/*[count(*[not(@inheritedFrom)]) != 0]" mode="toc-node"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="members" select="members/*/*[not(@inheritedFrom)]"/>
				<xsl:apply-templates select="$members" mode="toc-node">
					<xsl:with-param name="count" select="count($members)"/>
					<xsl:sort select="@sort" data-type="number" order="ascending" />
					<xsl:sort select="@name" data-type="text" order="ascending" />
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="*" mode="toc-node-item-type">
		<xsl:apply-templates select="." mode="item-type"/>
	</xsl:template>

	<xsl:template match="namespace" mode="toc-node-children" priority="10">
		<xsl:choose>
			<xsl:when test="$groupBy = 'type' and count(members/*) > 1">
				<xsl:apply-templates select="members/*" mode="toc-node"/>
			</xsl:when>
			<xsl:when test="$orderBy = 'type'">
				<xsl:variable name="members" select="members/*/*"/>
				<xsl:apply-templates select="$members" mode="toc-node">
					<xsl:with-param name="count" select="count($members)"/>
					<xsl:sort select="name(parent::node())"/>
					<xsl:sort select="@sort" data-type="number" order="ascending" />
					<xsl:sort select="@name" data-type="text" order="ascending" />
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="members" select="members/*/*"/>
				<xsl:apply-templates select="$members" mode="toc-node">
					<xsl:with-param name="count" select="count($members)"/>
					<xsl:sort select="@sort" data-type="number" order="ascending" />
					<xsl:sort select="@name" data-type="text" order="ascending" />
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="project" mode="toc-node-children" priority="10">
		<xsl:choose>
			<xsl:when test="count(members/namespaces/namespace) = 1 and members/namespaces/namespace/@name = 'global'">
				<xsl:variable name="members" select="members/namespaces/namespace/members/*"/>
				<xsl:apply-templates select="$members" mode="toc-node">
					<xsl:with-param name="count" select="count($members)"/>
					<xsl:sort select="@sort" data-type="number" order="ascending" />
					<xsl:sort select="@name" data-type="text" order="ascending" />
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="members" select="members/namespaces/namespace"/>
				<xsl:apply-templates select="$members" mode="toc-node">
					<xsl:with-param name="count" select="count($members)"/>
					<xsl:sort select="@sort" data-type="number" order="ascending" />
					<xsl:sort select="@name" data-type="text" order="ascending" />
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="class" mode="toc-node-children" priority="10">
		<xsl:choose>
			<xsl:when test="$groupBy = 'type' and count(members/*[count(*[not(@inheritedFrom)]) != 0]) > 1">
				<xsl:apply-templates select="members/*[count(*[not(@inheritedFrom)]) != 0]" mode="toc-node"/>
			</xsl:when>
			<xsl:otherwise>

				<xsl:variable name="members" select="members/*/*[not(@inheritedFrom)]"/>
				<xsl:variable name="functions" select="members/methods/method[not(@inheritedFrom) and @visibility!='static']"/>
				<xsl:variable name="properties" select="members/properties/property[not(@inheritedFrom) and @visibility!='static']"/>
				<xsl:variable name="functions-static" select="members/methods/method[not(@inheritedFrom) and @visibility='static']"/>
				<xsl:variable name="properties-static" select="members/properties/property[not(@inheritedFrom) and @visibility='static']"/>
				<xsl:variable name="events" select="members/events/event[not(@inheritedFrom)]"/>

				<!--
					The for-each construct is used to force the position() calculation to return the position in the reordered
					node context (properties, functions, events) as used in this template, rather than the ordinal position in
					the document.
				-->


				<xsl:for-each select="$properties-static">
					<xsl:sort select="@sort" data-type="number" order="ascending" />
					<xsl:sort select="@name" data-type="text" order="ascending" />
					<xsl:apply-templates select="." mode="toc-node">
						<xsl:with-param name="count" select="count($members)"/>
						<xsl:with-param name="position" select="position()"/>
					</xsl:apply-templates>
				</xsl:for-each>

				<xsl:for-each select="$properties">
					<xsl:sort select="@sort" data-type="number" order="ascending" />
					<xsl:sort select="@name" data-type="text" order="ascending" />
					<xsl:apply-templates select="." mode="toc-node">
						<xsl:with-param name="count" select="count($members)"/>
						<xsl:with-param name="position" select="position()"/>
					</xsl:apply-templates>
				</xsl:for-each>

				<xsl:for-each select="$functions-static">
					<xsl:sort select="@sort" data-type="number" order="ascending" />
					<xsl:sort select="@name" data-type="text" order="ascending" />
					<xsl:apply-templates select="." mode="toc-node">
						<xsl:with-param name="count" select="count($members)"/>
						<xsl:with-param name="position" select="count($properties) + position()"/>
					</xsl:apply-templates>
				</xsl:for-each>

				<xsl:for-each select="$functions">
					<xsl:sort select="@sort" data-type="number" order="ascending" />
					<xsl:sort select="@name" data-type="text" order="ascending" />
					<xsl:apply-templates select="." mode="toc-node">
						<xsl:with-param name="count" select="count($members)"/>
						<xsl:with-param name="position" select="count($properties) + position()"/>
					</xsl:apply-templates>
				</xsl:for-each>

				<xsl:for-each select="$events">
					<xsl:sort select="@sort" data-type="number" order="ascending" />
					<xsl:sort select="@name" data-type="text" order="ascending" />
					<xsl:apply-templates select="." mode="toc-node">
						<xsl:with-param name="count" select="count($members)"/>
						<xsl:with-param name="position" select="count($properties) + count($functions) + position()"/>
					</xsl:apply-templates>
				</xsl:for-each>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
